﻿using Client.Constant;
using Client.Enum;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Client.Model;
using Client.Service;
using Client.View;
using Client.View.CommonView;
using Client.View.OrgView;
using Client.View.PermissionView;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.ViewModel.OrgViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: OrgListViewModel
    /// CreateTime: 2020/5/28 19:06:45
    /// </summary>
    public class OrgListViewModel : BindableBase
    {
        public OrgListViewModel()
        {
            MsgEventSubscribe();
            Init();
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        private void Init()
        {
            AddOrgCommand = new DelegateCommand(AddOrg);
            RefreshOrgListCommand = new DelegateCommand(RefreshOrgList);
            PrePageCommand = new DelegateCommand(PrePage);
            NextPageCommand = new DelegateCommand(NextPage);
            EditOrgCommand = new DelegateCommand<ViewOrg>(EditOrg);
            DeleteOrgCommand = new DelegateCommand<ViewOrg>(DeleteOrg);
            OrgEdit = new OrgEdit();
            SelectedOrgCommand = new DelegateCommand<ViewOrg>(SelectedOrg);
            SearchCommand = new DelegateCommand(Search);
            ResetCommand = new DelegateCommand(Reset);

            OrgTreeUserControl = new OrgTreeNoBtn()
            {
                DataContext = this
            };
            OrgTree = OrgService.Instance().RootOrgTree;

            HasPrePage = false;
            HasNextPage = false;

            InitOrgs();
        }

        public void InitOrgs()
        {
            GetOrgs();
        }

        /// <summary>
        /// 消息事件订阅
        /// </summary>
        private void MsgEventSubscribe()
        {
            OrgService.Instance().SubscribeRefreshRootOrgTreeEvent(RefreshOrgTree);
            MessageEventPublisher.SubscribeEvent(MessageId.ORG_LIST, OrgListResponse);
            MessageEventPublisher.SubscribeEvent(MessageId.OPT_RESP, OptResp);
        }

        /// <summary>
        /// 组织列表
        /// </summary>
        private List<ViewOrg> orgs;
        public List<ViewOrg> Orgs
        {
            get { return orgs; }
            set
            {
                orgs = value;
                RaisePropertyChanged("Orgs");
            }
        }


        /// <summary>
        /// 新增编辑页面
        /// </summary>
        public OrgEdit OrgEdit { get; private set; }

        /// <summary>
        /// 当前页码
        /// </summary>
        private int currentPage = 1;

        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; RaisePropertyChanged("CurrentPage"); }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private readonly int LIMIT = 10;

        /// <summary>
        /// 是否有上一页
        /// </summary>
        private bool hasPrePage;

        public bool HasPrePage
        {
            get { return hasPrePage; }
            set
            {
                hasPrePage = value;
                RaisePropertyChanged("HasPrePage");
            }
        }



        /// <summary>
        /// 是否有下一页
        /// </summary>
        private bool hasNextPage;

        public bool HasNextPage
        {
            get { return hasNextPage; }
            set
            {
                hasNextPage = value;
                RaisePropertyChanged("HasNextPage");
            }
        }


        /// <summary>
        /// 新增组织
        /// </summary>
        public DelegateCommand AddOrgCommand { get; private set; }
        public void AddOrg()
        {
            OrgEdit.BindParentContextAndEntity(this);
            OrgEdit.ShowDialog();
        }

        /// <summary>
        /// 刷新组织列表
        /// </summary>
        public DelegateCommand RefreshOrgListCommand { get; private set; }
        public void RefreshOrgList()
        {
            GetOrgs();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        public DelegateCommand PrePageCommand { get; private set; }
        public void PrePage()
        {
            if (hasPrePage)
            {
                CurrentPage--;
                GetOrgs();
            }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        public DelegateCommand NextPageCommand { get; private set; }
        public void NextPage()
        {
            if (hasNextPage)
            {
                CurrentPage++;
                GetOrgs();
            }
        }

        /// <summary>
        /// 编辑组织信息
        /// </summary>
        public DelegateCommand<ViewOrg> EditOrgCommand { get; private set; }
        public void EditOrg(ViewOrg org)
        {
            OrgEdit.BindParentContextAndEntity(this, org);
            OrgEdit.ShowDialog();
        }

        /// <summary>
        /// 删除组织信息
        /// </summary>
        public DelegateCommand<ViewOrg> DeleteOrgCommand { get; private set; }
        private int deleteOrgId;
        public void DeleteOrg(ViewOrg org)
        {
            this.deleteOrgId = org.ID;
            CancelConfirmTip tip = new CancelConfirmTip()
            {
                Message = { Text = $"确定删除组织{org.OrgName}吗？" }
            };
            DialogHost.Show(tip, "MainPanel", CloseTip);
        }

        public void CloseTip(object sender, DialogClosingEventArgs eventArgs)
        {
            if (string.Equals(eventArgs.Parameter, "Cancel")) return;
            DeleteOrgReq msg = new DeleteOrgReq(MessageId.DELETE_ORG)
            {
                ID = deleteOrgId
            };
            ApplicationClient.Instance().Send(msg);
        }

        private void OptResp(ResponseBaseMessage message)
        {
            if (message is OperationResp msg)
            {
                if (msg.Code == 200 && msg.OptType == (int)OptType.DELETE_ORG)
                {
                    OrgService.Instance().RequestOrgTree();
                    GetOrgs();
                }
            }
        }


        /// <summary>
        /// 获取组织列表
        /// </summary>
        public void GetOrgs()
        {
            OrgListReq msg = new OrgListReq(MessageId.REQUEST_ORG_LIST)
            {
                Page = CurrentPage,
                Limit = LIMIT,
                Condition = Condition
            };
            ApplicationClient.Instance().Send(msg);
        }


        /// <summary>
        /// 列表信息返回
        /// </summary>
        /// <param name="message">返回的消息</param>
        private void OrgListResponse(ResponseBaseMessage message)
        {
            if (message is OrgListResp msg)
            {
                Orgs = msg.Orgs;
                HasPrePage = CurrentPage > 1;
                HasNextPage = CurrentPage < (int)Math.Ceiling((double)msg.Count / LIMIT);
            }
        }


        private ViewOrg condition = new ViewOrg() { Status = 0, ParentId = 9999 };

        public ViewOrg Condition
        {
            get { return condition; }
            set
            {
                condition = value;
                RaisePropertyChanged("Condition");
            }
        }

        public DelegateCommand SearchCommand { get; set; }
        private void Search()
        {
            GetOrgs();
        }

        public DelegateCommand ResetCommand { get; set; }
        private void Reset()
        {
            OrgTreeUserControl.ResetSelectedStatus();
            Condition.ParentId = 9999;
            Condition.OrgName = string.Empty;
            Condition.Status = 0;
            GetOrgs();
        }




        public OrgTreeNoBtn OrgTreeUserControl { get; set; }

        private void RefreshOrgTree(List<ViewOrg> orgTree)
        {
            OrgTree = orgTree;
        }

        private List<ViewOrg> orgTree;

        public List<ViewOrg> OrgTree
        {
            get { return orgTree; }
            set
            {
                orgTree = value;
                RaisePropertyChanged("OrgTree");
            }
        }


        public DelegateCommand<ViewOrg> SelectedOrgCommand { get; set; }
        private void SelectedOrg(ViewOrg org)
        {
            if (org == null) return;
            Condition.ParentId = org.ID;
            GetOrgs();
        }






    }
}
